// pages/my/login/Register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:'',
    userUserName:'',
    userPassword:'',
    userNickName:'',
    userEmail:'',
    userPhone:'',
    userAddress:''
  },
  userName(e) {
    this.setData({
      userName: e.detail.value
    });
  },
  userUserName(e) {
    this.setData({
      userUserName: e.detail.value
    });
  },
  userPassword(e) {
    this.setData({
      userPassword: e.detail.value
    });
  },
  userNickName(e) {
    this.setData({
      userNickName: e.detail.value
    });
  },
  userEmail(e) {
    this.setData({
      userEmail: e.detail.value
    });
  },
  userPhone(e) {
    this.setData({
      userPhone: e.detail.value
    });
  },
  userAddress(e) {
    this.setData({
      userAddress: e.detail.value
    });
  },
  // 注册按钮
  submitModify(){
    const { userName,userUserName, userPassword, userEmail, userPhone,userNickName,userAddress } = this.data;
        // 用户名不能为空
        if (!userName) {
            wx.showToast({
                title: '用户名不能为空',
                icon: 'none'
            });
            return;
        }
         // 用户昵称不能为空
         if (!userUserName) {
          wx.showToast({
              title: '用户昵称不能为空',
              icon: 'none'
          });
          return;
          }
          // 地址不能为空
         if (!userAddress) {
          wx.showToast({
              title: '地址不能为空',
              icon: 'none'
          });
          return;
          }
          // 用户账号不能为空
         if (!userUserName) {
          wx.showToast({
              title: '用户账号不能为空',
              icon: 'none'
          });
          return;
          }
        // 密码长度验证
        if (userPassword.length < 6) {
            wx.showToast({
                title: '密码长度不能少于 6 位',
                icon: 'none'
            });
            return;
        }

        // 邮箱格式验证
        const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        if (userEmail && !emailRegex.test(userEmail)) {
            wx.showToast({
                title: '邮箱格式不正确',
                icon: 'none'
            });
            return;
        }

        // 手机号格式验证
        const phoneRegex = /^1[3-9]\d{9}$/;
        if (userPhone && !phoneRegex.test(userPhone)) {
            wx.showToast({
                title: '手机号格式不正确',
                icon: 'none'
            });
            return;
        }
        wx.request({
          url: 'http://localhost:8080/user/add',
          method: 'POST',
          data:{
            userName:this.data.userName,
            userUserName:this.data.userUserName,
            userPassword:this.data.userPassword,
            userNickName:this.data.userNickName,
            userEmail:this.data.userEmail,
            userPhone:this.data.userPhone,
            userAddress:this.data.userAddress,
            userPoint:0
          },
          success: (res) => {
            if(res.data.code===1){
              wx.showToast({
                title: '注册成功',
                icon: 'success',
                success: () => {
                    setTimeout(() => {
                        wx.reLaunch({
                            url: '/pages/my/login/login'
                        });
                    }, 1000);
                }
            });
            }
        },
        })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

})